package de.tum.in.eist.poll.client.widgets.other;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;

import de.tum.in.eist.poll.client.PollService;
import de.tum.in.eist.poll.client.PollServiceAsync;

/**
 * A Button that logs out the current user when clicked
 * @author aperture
 *
 */
public class LogoutButton extends Button{
	private PollServiceAsync pollService = GWT.create(PollService.class);
	
	public LogoutButton() {
		this.setText("Logout");
		
		this.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				pollService.logout(new AsyncCallback<Void>() {
					@Override
					public void onSuccess(Void result) {
						Window.open("../?gwt.codesvr=127.0.0.1:9997", "_self", "");
					}

					@Override
					public void onFailure(Throwable caught) {
						// TODO Check this line
					}
				});
			}
		});
	}
}
